package com.umbrella.worker.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.umbrella.worker.dao.WMemberCouponMapper;
import com.umbrella.worker.dao.WOrderDetailMapper;
import com.umbrella.worker.dao.WOrderMapper;
import com.umbrella.worker.dao.WSmsRecordMapper;
import com.umbrella.worker.dao.WStaffMapper;
import com.umbrella.worker.dao.WStaffTimeMapper;
import com.umbrella.worker.dao.WSupplierAccountMapper;
import com.umbrella.worker.dao.WSupplierMapper;
import com.umbrella.worker.dto.AccountRecordDO;
import com.umbrella.worker.dto.OrderDO;
import com.umbrella.worker.dto.OrderDetailDO;
import com.umbrella.worker.dto.PayrecordDO;
import com.umbrella.worker.entity.WMemberCoupon;
import com.umbrella.worker.entity.WOrder;
import com.umbrella.worker.entity.WOrderDetail;
import com.umbrella.worker.entity.WOrderExample;
import com.umbrella.worker.entity.WSmsRecord;
import com.umbrella.worker.entity.WStaff;
import com.umbrella.worker.entity.WStaffTime;
import com.umbrella.worker.entity.WStaffTimeExample;
import com.umbrella.worker.entity.WSupplier;
import com.umbrella.worker.entity.WSupplierAccount;
import com.umbrella.worker.entity.WSupplierExample;
import com.umbrella.worker.query.OrderQuery;
import com.umbrella.worker.result.ResultDO;
import com.umbrella.worker.result.ResultSupport;
import com.umbrella.worker.service.IOrderService;
import com.umbrella.worker.service.IPayService;
import com.umbrella.worker.service.ISMSGatewayService;
import com.umbrella.worker.service.ISupplierAccountService;
import com.umbrella.worker.util.BeanUtilsExtends;
import com.umbrella.worker.util.Constant;
import com.umbrella.worker.util.MakeOrderNum;
import com.umbrella.worker.util.StringUtil;
@Service("orderService")
public class OrderServiceImpl  extends BaseServiceImpl implements IOrderService {
	
	private static Logger logger = Logger.getLogger(OrderServiceImpl.class);
	@Autowired
	private WOrderMapper orderMapper;
	@Autowired
	private WOrderDetailMapper orderDetailMapper;
	@Autowired
	private IPayService payService;
	@Autowired
	private ISMSGatewayService smsGatewayService;
	@Autowired
	private WSupplierMapper supplierMapper;
	@Autowired
	private WSupplierAccountMapper supplierAccountMapper;
	@Autowired
	private WSmsRecordMapper smsRecordMapper;
	@Autowired
	private WStaffMapper staffMapper;
	@Autowired
	private  WStaffTimeMapper staffTimeMapper;
	@Autowired
	private WMemberCouponMapper memberCouponMapper;
	@Autowired
	private ISupplierAccountService supplierAccountService;
	
	@PostConstruct  
	public void orderInit() {
		new Thread(new OrderCheck()).start();
		new Thread(new Settle()).start();
		new Thread(new AssignedThread()).start();
	}
	@Transactional
	@Override
	public ResultDO create(OrderDO orderDO) {
		
		WOrder order = new WOrder();
		
		WOrderDetail orderDetail = new WOrderDetail();
		
		ResultSupport result = BeanUtilsExtends.copy(order, orderDO);
		
		if(!result.isSuccess()) {
			return result;
		}
		
		int recordNum = -1;

		
		MakeOrderNum makeOrder = new MakeOrderNum();  
        String orderNO = makeOrder.makeOrderNum();
        if(StringUtil.isEmpty(orderNO)) {
        	result.setSuccess(false);
			return result;
        }
        
        order.setwOOrderNo(orderNO);
		
		order.setDatalevel(1);
		if(order.getStatus()==null){
			order.setStatus(1);
		}
		order.setwOIsEnd(0);
		order.setwOIsPay(0);
		order.setwOIsConfim(0);
		order.setCreateTime(Calendar.getInstance().getTime());
		order.setModifiTime(Calendar.getInstance().getTime());
		order.setModifiAuthor(order.getCreateAuthor());
		
		try {
			recordNum = orderMapper.insertSelective(order);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			logger.error("[obj:order][opt:create][msg:" + e.getMessage()
			+ "]");
			e.printStackTrace();
			return result;
		}
		
		if(recordNum < 1) {
			result.setSuccess(false);
			return result;
		}
		
		result = BeanUtilsExtends.copy(orderDetail, orderDO.getOrderDetailDO());
		
		if(!result.isSuccess()) {
			return result;
		}
		
		orderDetail.setId(order.getId());
		orderDetail.setCreateTime(order.getCreateTime());
		orderDetail.setCreateAuthor(order.getCreateAuthor());
		orderDetail.setModifiTime(order.getCreateTime());
		orderDetail.setModifiAuthor(order.getCreateAuthor());
		orderDetail.setDatalevel(1);
		
		
		recordNum = -1;
		try {
			recordNum = orderDetailMapper.insertSelective(orderDetail);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			logger.error("[obj:order][opt:create][msg:" + e.getMessage()
			+ "]");
			e.printStackTrace();
			return result;
		}
	
		
		result.setModel(ResultDO.FIRST_MODEL_KEY, order.getId());
		
		return result;
	}
	@Transactional
	@Override
	public ResultDO modifi(OrderDO orderDO) {
		
		WOrder order = new WOrder();

		ResultSupport result = BeanUtilsExtends.copy(order, orderDO);
		// 拷贝失败
		if (!result.isSuccess()) {
			return result;
		}
		order.setModifiTime(Calendar.getInstance().getTime());
		
		int recordNum = -1;
		try {
			recordNum = orderMapper.updateByPrimaryKeySelective(order);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			logger.error("[obj:order][opt:modifi][msg:" + e.getMessage()
					+ "]");
			e.printStackTrace();
			return result;
		}
		if (recordNum < 1) {
			result.setSuccess(false);
		}
		if(orderDO.getOrderDetailDO()!=null){
		if(!StringUtil.isGreatOne(orderDO.getOrderDetailDO().getId())) {
			return result;
		}
		WOrderDetail orderDetail = new WOrderDetail();

		result = BeanUtilsExtends.copy(orderDetail, orderDO.getOrderDetailDO());
		// 拷贝失败
		if (!result.isSuccess()) {
			return result;
		}
		
		recordNum = -1;
		try {
			recordNum = orderDetailMapper.updateByPrimaryKeySelective(orderDetail);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			logger.error("[obj:order][opt:modifi][msg:" + e.getMessage()
					+ "]");
			e.printStackTrace();
			return result;
		}
		if (recordNum < 1) {
			result.setSuccess(false);
		}

		return result;
		}
		return result;
	}
	@Transactional
	public ResultDO assigned(OrderDO orderDO) {
		WOrder order = new WOrder();

		ResultSupport result = BeanUtilsExtends.copy(order, orderDO);
		// 拷贝失败
		if (!result.isSuccess()) {
			return result;
		}
		order.setModifiTime(Calendar.getInstance().getTime());
		order.setwAssigendTime(Calendar.getInstance().getTime());
		
		int recordNum = -1;
		try {
			recordNum = orderMapper.updateByPrimaryKeySelective(order);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			logger.error("[obj:order][opt:modifi][msg:" + e.getMessage()
					+ "]");
			e.printStackTrace();
			return result;
		}
		if (recordNum < 1) {
			result.setSuccess(false);
		}
		
		if(!StringUtil.isGreatOne(orderDO.getOrderDetailDO().getId())) {
			return result;
		}
		WOrderDetail orderDetail = new WOrderDetail();
		result = BeanUtilsExtends.copy(orderDetail, orderDO.getOrderDetailDO());
		// 拷贝失败
		if (!result.isSuccess()) {
			return result;
		}
		
		recordNum = -1;
		try {
			recordNum = orderDetailMapper.updateByPrimaryKeySelective(orderDetail);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			logger.error("[obj:order][opt:modifi][msg:" + e.getMessage()
					+ "]");
			e.printStackTrace();
			return result;
		}
		
		try {
			order = orderMapper.selectByPrimaryKey(order.getId());
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			logger.error("[obj:order][opt:modifi][msg:" + e.getMessage()
					+ "]");
			e.printStackTrace();
			return result;
		}
		
		try {
			orderDetail = orderDetailMapper.selectByPrimaryKey(order.getId());
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			logger.error("[obj:order][opt:modifi][msg:" + e.getMessage()
					+ "]");
			e.printStackTrace();
			return result;
		}
		String staffName = null;
		String staffTelephone = null;
		
		for(Integer staffId : orderDO.getStaffIds()) {
			
			WStaff staff = staffMapper.selectByPrimaryKey(staffId);
			WStaffTime staffTime = new WStaffTime();
			staffTime.settStaffId(staff.getId());
			staffTime.settOrderId(order.getId());
			staffTime.settStaffName(staff.getwSName());
			staffTime.settStaffMobile(staff.getwSTelephone());
			
			staffTime.settSupplierId(staff.getwSSupplierId());
			staffTime.settOrderno(order.getwOOrderNo());
			staffTime.settServiceName(order.getwOServiceName());
			if(orderDetail!=null){
				staffTime.settMemberMobile(orderDetail.getwOTelephone());
				staffTime.settMemberName(orderDetail.getwOContact());
				
				staffTime.settStartTime(orderDetail.getwOSubscribe());
				staffTime.settServimeTime(orderDetail.getwOServerTime());
				
				if(orderDetail.getwOSubscribe()!=null && orderDetail.getwOServerTime()!=null){
					
					staffTime.settEndTime(new Date(orderDetail.getwOSubscribe().getTime() + (orderDetail.getwOServerTime() * 60 * 60 * 1000)));
				}
			}
			if(orderDO.getStaffIds().size() > 1) {
				staffTime.settFee(order.getwOFee().divide(new BigDecimal(orderDO.getStaffIds().size()), 3, RoundingMode.HALF_UP) );
			} else {
				staffTime.settFee(order.getwOFee());
			}
			staffTime.setCreateAuthor("system");
			staffTime.setModifiAuthor("system");
			staffTime.setCreateTime(Calendar.getInstance().getTime());
			staffTime.setModifiTime(staffTime.getCreateTime());
			//1.正在工作 2.已结束工作
			staffTime.setStatus(1);
			staffTime.setDatalevel(1);
			staffName = staff.getwSName();
			staffTelephone = staff.getwSTelephone();
			try {
				staffTimeMapper.insertSelective(staffTime);
			} catch (Exception e) {
				result.setSuccess(false);
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
				logger.error("[obj:order][opt:modifi][msg:" + e.getMessage()
						+ "]");
				e.printStackTrace();
				return result;
			}
			recordNum = -1;
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String start1="";
			if(staffTime.gettStartTime()!=null){
				start1=format.format(staffTime.gettStartTime());
			}
			String start2="";
			if(staffTime.gettEndTime()!=null){
				start2=format.format(staffTime.gettEndTime());
			}
			String arg = "从" + start1 + "至" + start2 + "到" + orderDetail.getwOAddress() + "进行" +  order.getwOServiceName() + "任务,请和客户" + orderDetail.getwOContact() + "联系，电话号码" + orderDetail.getwOTelephone();
			WSmsRecord smsRecord = new WSmsRecord();
			smsRecord.setsSupplierId(orderDO.getwOSupplierId());
			smsRecord.setsMobile(staff.getwSTelephone());
			smsRecord.setsFee(new BigDecimal(0.08));
			smsRecord.setsReason("工作通知");
			smsRecord.setCreateAuthor("system");
			smsRecord.setModifiAuthor(smsRecord.getCreateAuthor());
			smsRecord.setCreateTime(Calendar.getInstance().getTime());
			smsRecord.setModifiTime(smsRecord.getCreateTime());
			smsRecord.setStatus(1);
			smsRecord.setDatalevel(1);
			try {
				recordNum = smsRecordMapper.insertSelective(smsRecord);
			} catch (Exception e) {
				
				logger.error("[obj:order][opt:modifi][msg:" + e.getMessage()
						+ "]");
				e.printStackTrace();
			}
			if(recordNum > 0) {
				smsGatewayService.initSMS();
				//ResultDO resultDO = smsGatewayService.send(staff.getwSTelephone(), Constant.SMS_STAFF_ASSIGNED_KEY, arg, 1);
				ResultDO resultDO = smsGatewayService.send(staff.getwSTelephone(), Constant.SMS_ORDER_ASSIGNED_KEY, arg, 1);
				if(!resultDO.isSuccess()) {
					result.setSuccess(false);
				}
			} else {
				result.setSuccess(false);
			}
			
			WStaff s = new WStaff();
			s.setId(staff.getId());
			s.setStatus(2);
			s.setwSWorkerStatus(1);
			try {
				staffMapper.updateByPrimaryKeySelective(s);
			} catch (Exception e) {
				result.setSuccess(false);
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
				logger.error("[obj:order][opt:modifi][msg:" + e.getMessage()
						+ "]");
				e.printStackTrace();
				return result;
			}
		}
		String arg = "";
		if(order.getwOServiceType() == 0) {
			arg = staffName + ",联系电话" + staffTelephone + ",本次服务由" + orderDetail.getwOSupplierName() + "为您提供！客服热线 400-870-2588";
		} else {
			arg = orderDetail.getwOSupplierName() + "为您提供服务！客服热线 400-870-2588";
		}
		WSmsRecord smsRecord = new WSmsRecord();
		smsRecord.setsSupplierId(orderDO.getwOSupplierId());
		smsRecord.setsMobile(orderDetail.getwOTelephone());
		smsRecord.setsFee(new BigDecimal(0.08));
		smsRecord.setsReason("客户通知");
		smsRecord.setCreateAuthor("system");
		smsRecord.setModifiAuthor(smsRecord.getCreateAuthor());
		smsRecord.setCreateTime(Calendar.getInstance().getTime());
		smsRecord.setModifiTime(smsRecord.getCreateTime());
		smsRecord.setStatus(1);
		smsRecord.setDatalevel(1);
		try {
			smsRecordMapper.insertSelective(smsRecord);
		} catch (Exception e) {
			
			logger.error("[obj:order][opt:modifi][msg:" + e.getMessage()
					+ "]");
			e.printStackTrace();
		}
		if(recordNum > 0) {
			//smsGatewayService.initSMS();
			String smsKey = Constant.SMS_ORDER_ASSIGNED_KEY; 
			System.out.println(orderDO.getwOServiceType());
			/*switch(order.getwOServiceType()) {
				case 0: {
					smsKey = Constant.SMS_ORDER_ASSIGNED_KEY;
					break;
				}
				case 1: {
					smsKey = Constant.SMS_ORDER2_ASSIGNED_KEY;
					break;
				}
				case 2: {
					smsKey = Constant.SMS_ORDER2_ASSIGNED_KEY;
					break;
				}
				case 3: {
					smsKey = Constant.SMS_ORDER2_ASSIGNED_KEY;
					break;
				}
			}*/
			
			ResultDO resultDO = smsGatewayService.send(orderDetail.getwOTelephone(), smsKey, arg, 1);
			if(!resultDO.isSuccess()) {
				result.setSuccess(false);
			}
		} else {
			result.setSuccess(false);
		}
		
		return result;
	}
	@Transactional
	@Override
	public ResultDO updateStatus(OrderDO orderDO) {
		
		WOrderExample example = new WOrderExample();
		ResultSupport result = new ResultSupport();
		example.createCriteria().andWOOrderNoEqualTo(orderDO.getwOOrderNo());
		List<WOrder> list;
		
		try {
			list = orderMapper.selectByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			logger.error("[obj:order][opt:modifi][msg:" + e.getMessage()
					+ "]");
			e.printStackTrace();
			return result;
		}
		
		WOrder order = list.get(0);
		order.setwOIsPay(1);
		order.setStatus(2);
		order.setModifiAuthor("system");
		order.setModifiTime(Calendar.getInstance().getTime());
		AccountRecordDO accountRecordDO = new AccountRecordDO();
		accountRecordDO.setwType(1);
		accountRecordDO.setwItem("订单支付");
		accountRecordDO.setwMoney(order.getwOFee());
		accountRecordDO.setwDesc("订单号：" + order.getwOOrderNo() + "支付成功");
		accountRecordDO.setModifiAuthor("system");
		accountRecordDO.setCreateTime(Calendar.getInstance().getTime());
		accountRecordDO.setCreateAuthor("system");
		accountRecordDO.setStatus(1);
		accountRecordDO.setDatalevel(1);
		supplierAccountService.createRecrod(accountRecordDO);
		if(orderDO.getCouponId() != null) {
		
			WMemberCoupon memberCoupon = null;
			try {
				memberCoupon = memberCouponMapper.selectByPrimaryKey(orderDO.getCouponId());
			} catch (Exception e) {
				result.setSuccess(false);
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
				logger.error("[obj:order][opt:modifi][msg:" + e.getMessage()
						+ "]");
				e.printStackTrace();
				return result;
			}
			BigDecimal fee = order.getwOFee();
			if(memberCoupon.getwMcCouponType() == 1) {
				order.setwOCoupon(memberCoupon.getwMcMoney());
			} else {
				float discount = memberCoupon.getwMcDiscount();
				order.setwOCoupon(fee.divide(fee.multiply(new BigDecimal(discount))));
			}
		}
		int recordNum = -1;
		try {
			recordNum = orderMapper.updateByExampleSelective(order, example);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			logger.error("[obj:order][opt:modifi][msg:" + e.getMessage()
					+ "]");
			e.printStackTrace();
			return result;
		}
		if (recordNum < 1) {
			result.setSuccess(false);
		}
		
		PayrecordDO payrecordDO = new PayrecordDO();
		payrecordDO.setwPrOrderNo(orderDO.getwOOrderNo());
		ResultDO resultDO = null;
		resultDO = payService.updateStatus(payrecordDO);
		
		if(!resultDO.isSuccess()) {
			result.setSuccess(false);
		}
		
		result.setModel(ResultSupport.FIRST_MODEL_KEY, order.getId());
		
		return result;
	}
	@Transactional
	@Override
	public ResultDO modifiDetail(OrderDetailDO orderDetailDO) {
		
		WOrderDetail orderDetail = new WOrderDetail();

		ResultSupport result = BeanUtilsExtends.copy(orderDetail, orderDetailDO);
		// 拷贝失败
		if (!result.isSuccess()) {
			return result;
		}
		
		int recordNum = -1;
		try {
			recordNum = orderDetailMapper.updateByPrimaryKeySelective(orderDetail);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			logger.error("[obj:order][opt:modifi][msg:" + e.getMessage()
					+ "]");
			return result;
		}
		if (recordNum < 1) {
			result.setSuccess(false);
		}

		return result;
	}
	@Transactional
	@Override
	public ResultDO remove(int orderId) {
		
		ResultSupport result = new ResultSupport();
		
		WOrder order = new WOrder();
		
		if(StringUtil.isGreatOne(orderId)) {
			order.setId(orderId);
		} else {
		    result.setSuccess(false);
		    return result;
		}
		order.setDatalevel(-1);
		int recordNum = -1;
		try {
			recordNum = orderMapper.updateByPrimaryKeySelective(order);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:order][opt:remove][msg:"+e.getMessage()+"]");
	        return result;
		}
		if (recordNum != 1) {
			result.setSuccess(false);
		}
		return result;
	}
	
	@Transactional
	@Override
	public ResultDO rush(OrderDO orderDO) {
	
		WOrder order = new WOrder();

		ResultSupport result = BeanUtilsExtends.copy(order, orderDO);
		// 拷贝失败
		if (!result.isSuccess()) {
			return result;
		}
		
		order.setModifiTime(Calendar.getInstance().getTime());
		
		int recordNum = -1;
	
		try {
			recordNum = orderMapper.updateByPrimaryKeySelective(order);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			logger.error("[obj:order][opt:modifi][msg:" + e.getMessage()
					+ "]");
			e.printStackTrace();
			return result;
		}
		
		System.out.println(recordNum);
		if (recordNum < 1) {
			result.setSuccess(false);
		}
		
		recordNum = -1;
		WOrderDetail orderDetail = new WOrderDetail();
		result = BeanUtilsExtends.copy(orderDetail, orderDO.getOrderDetailDO());
		// 拷贝失败
		if (!result.isSuccess()) {
			return result;
		}
		
		orderDetail.setModifiTime(order.getModifiTime());
		
		try {
			recordNum = orderDetailMapper.updateByPrimaryKeySelective(orderDetail);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			logger.error("[obj:order][opt:modifi][msg:" + e.getMessage()
					+ "]");
			e.printStackTrace();
			return result;
		}
		System.out.println(recordNum);
		if (recordNum < 1) {
			result.setSuccess(false);
		}
		
		return result;
		
	}
	@Transactional
	@Override
	public ResultDO confirm(OrderDO orderDO) {
		
		ResultSupport result = new ResultSupport();
		
		WOrder order = new WOrder();
		
		result = BeanUtilsExtends.copy(order, orderDO);
		
		if(!result.isSuccess()) {
			 return result;
		} 
		
		order.setModifiTime(Calendar.getInstance().getTime());
		int recordNum = -1;
		try {
			recordNum = orderMapper.updateByPrimaryKeySelective(order);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
	        e.printStackTrace();
	        return result;
		}
		
		if(recordNum < 1) {
			result.setSuccess(false);
			return result;
		}
		
		
		try {
			order = orderMapper.selectByPrimaryKey(order.getId());
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
	        e.printStackTrace();
	        return result;
		}
		
		WStaffTimeExample wstex = new WStaffTimeExample();
		wstex.createCriteria().andTOrdernoEqualTo(order.getwOOrderNo());
		
		List<WStaffTime> listStaff = null;
		try {
			listStaff = staffTimeMapper.selectByExample(wstex);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
	        e.printStackTrace();
	        return result;
		}
		
		for(WStaffTime staffTime : listStaff) {
			staffTime.setStatus(2);
			try {
				staffTimeMapper.updateByPrimaryKeySelective(staffTime);
			} catch (Exception e) {
				result.setSuccess(false);
		        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
		        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
		        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
		        e.printStackTrace();
		        return result;
			}
			WStaff staff = new WStaff();
			staff.setId(staffTime.gettStaffId());
			staff.setStatus(1);
			try {
				staffMapper.updateByPrimaryKeySelective(staff);
			} catch (Exception e) {
				result.setSuccess(false);
		        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
		        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
		        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
		        e.printStackTrace();
		        return result;
			}
		}
		return result;
	}


	@Override
	public ResultDO get(int orderId) {
		
		ResultSupport result = new ResultSupport();
		
		WOrder order = null;
		if(!StringUtil.isGreatOne(orderId)) {
			 result.setSuccess(false);
			 return result;
		} 
		
		try {
			order = orderMapper.selectByPrimaryKey(orderId);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		WOrderDetail orderDetail = null;
		
		try {
			orderDetail = orderDetailMapper.selectByPrimaryKey(orderId);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
	        e.printStackTrace();
	        return result;
		}
		
		OrderDetailDO orderDetailDO = getOrderDetailDO(orderDetail);
		
		OrderDO orderDO = getOrderDO(order);
		if(orderDO != null) {
			orderDO.setOrderDetailDO(orderDetailDO);
			if(orderDO.getStatus() >= 3) {
				
				WSupplier supplier = supplierMapper.selectByPrimaryKey(orderDO.getwOSupplierId());
				orderDetailDO.setSupplierTel(supplier.getwSTelephone());
				
			}
		} else {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			return result;
		}

		result.setModel(ResultSupport.FIRST_MODEL_KEY, orderDO);
		return result;
	}
	
	@Override
	public ResultDO assignedList(OrderQuery orderQuery) {
		
		ResultSupport result = new ResultSupport();
		
		WOrderExample example = new WOrderExample();
		WOrderExample.Criteria c = example.createCriteria();
		
		if(orderQuery.getStartDate()!=null  ) {
			c.andCreateTimeGreaterThanOrEqualTo(orderQuery.getStartDate());
		}
		if(orderQuery.getEndDate()!=null){
			c.andCreateTimeLessThanOrEqualTo(orderQuery.getEndDate());
		}
		
		if(StringUtil.isGreatOne(orderQuery.getSupplierId())) {
			c.andWOSupplierIdEqualTo(orderQuery.getSupplierId());
		}
        c.andWONeedIdIsNull();
		c.andStatusEqualTo(8);
	

		if(StringUtil.isNotEmpty(orderQuery.getOrderByClause())) {	
			example.setOrderByClause(" " + orderQuery.getOrderByClause() + " " + orderQuery.getSort());
		} else {
			example.setOrderByClause(" CREATE_TIME DESC");
		}
		
		c.andDatalevelEqualTo(1);
		
		if(orderQuery.isPage()) {
			long count = 0;
			try {
				count = orderMapper.countByExample(example);
			} catch (Exception e) {
				result.setSuccess(false);
		        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
		        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
		        e.printStackTrace();
		        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
		        return result;
			}
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
			int pageNO = orderQuery.getPageNO();
			if(pageNO > 0) {
				pageNO = pageNO -1;
			}
			String pageByClause = " limit " + (pageNO * orderQuery.getPageRows())
					+ "," + orderQuery.getPageRows();
			
			
			example.setPageByClause(pageByClause);
		}
		
		List<WOrder> list = null;
		
		try {
			list = orderMapper.selectByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		List<OrderDO> orderList = getOrderDOList(list);
		
		WOrderDetail orderDetail = null;
		List<OrderDO> list2 = new ArrayList<OrderDO>();
		if(orderList != null) {
			for(OrderDO order : orderList) {
				try {
					orderDetail = orderDetailMapper.selectByPrimaryKey(order.getId());
				} catch (Exception e) {
					result.setSuccess(false);
			        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
			        return result;
				}
				order.setOrderDetailDO(getOrderDetailDO(orderDetail));
				list2.add(order);
			}
		}
		result.setModel(ResultSupport.FIRST_MODEL_KEY, list2);
		
		return result;
	}
	
	@Override
	public ResultDO adminAssignedList(OrderQuery orderQuery) {
		
		ResultSupport result = new ResultSupport();
		
		WOrderExample example = new WOrderExample();
		WOrderExample.Criteria c = example.createCriteria();
		List<Integer> slist = new ArrayList<Integer>();
		slist.add(1);
		slist.add(2);
		slist.add(3);
		c.andWOServiceTypeIn(slist);
		c.andStatusEqualTo(2);
	

		if(StringUtil.isNotEmpty(orderQuery.getOrderByClause())) {	
			example.setOrderByClause(" " + orderQuery.getOrderByClause() + " " + orderQuery.getSort());
		} else {
			example.setOrderByClause(" CREATE_TIME DESC");
		}
		
		c.andDatalevelEqualTo(1);
		
		if(orderQuery.isPage()) {
			long count = 0;
			try {
				count = orderMapper.countByExample(example);
			} catch (Exception e) {
				result.setSuccess(false);
		        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
		        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
		        e.printStackTrace();
		        logger.error("[obj:member][opt:get][msg:"+e.getMessage()+"]");
		        return result;
			}
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
			int pageNO = orderQuery.getPageNO();
			if(pageNO > 0) {
				pageNO = pageNO -1;
			}
			String pageByClause = " limit " + (pageNO * orderQuery.getPageRows())
					+ "," + orderQuery.getPageRows();
			
			
			example.setPageByClause(pageByClause);
		}
		
		List<WOrder> list = null;
		
		try {
			list = orderMapper.selectByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		List<OrderDO> orderList = getOrderDOList(list);
		WOrderDetail orderDetail = null;
		List<OrderDO> list2 = new ArrayList<OrderDO>();
		if(orderList != null) {
			for(OrderDO order : orderList) {
				try {
					orderDetail = orderDetailMapper.selectByPrimaryKey(order.getId());
				} catch (Exception e) {
					result.setSuccess(false);
			        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
			        return result;
				}
				order.setOrderDetailDO(getOrderDetailDO(orderDetail));
				list2.add(order);
			}
		}
		
		result.setModel(ResultSupport.FIRST_MODEL_KEY, list2);
		
		return result;
	}

	@Override
	public ResultDO list(OrderQuery orderQuery) {
		
		ResultSupport result = new ResultSupport();
		
		WOrderExample example = new WOrderExample();
		WOrderExample.Criteria c = example.createCriteria();
		
		if(StringUtil.isGreatOne(orderQuery.getMemberId())) {
			c.andWOMembersIdEqualTo(orderQuery.getMemberId());
		}
		if(orderQuery.getServiceType() != null) {
			if(orderQuery.getServiceType() != -1) {
				c.andWOServiceTypeEqualTo(orderQuery.getServiceType());
			}
		}
		
		if(StringUtil.isNotEmpty(orderQuery.getOrderNo())) {
			c.andWOOrderNoEqualTo(orderQuery.getOrderNo());
		}
		if(orderQuery.getSupplierId() != null) {
			if(orderQuery.getSupplierId() != 1) {
				if(StringUtil.isGreatOne(orderQuery.getSupplierId())) {
					c.andWOSupplierIdEqualTo(orderQuery.getSupplierId());
				}
			}
		}
		System.out.println(orderQuery.getStatus());
		if(StringUtil.isGreatOne(orderQuery.getStatus())) {
			if(orderQuery.getStatus() == 58) {
				List<Integer> params = new ArrayList<Integer>();
				params.add(5);
				params.add(8);
				c.andStatusIn(params);
			} else {
			
				c.andStatusEqualTo(orderQuery.getStatus());
			}
		}

		if(StringUtil.isNotEmpty(orderQuery.getOrderByClause())) {	
			example.setOrderByClause(" " + orderQuery.getOrderByClause() + " " + orderQuery.getSort());
		} else {
			example.setOrderByClause(" CREATE_TIME DESC");
		}
		
		c.andDatalevelEqualTo(1);
		
		if(orderQuery.isPage()) {
			long count = 0;
			try {
				count = orderMapper.countByExample(example);
			} catch (Exception e) {
				result.setSuccess(false);
		        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
		        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
		        e.printStackTrace();
		        logger.error("[obj:member][opt:get][msg:"+e.getMessage()+"]");
		        return result;
			}
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
			int pageNO = orderQuery.getPageNO();
			if(pageNO > 0) {
				pageNO = pageNO -1;
			}
			String pageByClause = " limit " + (pageNO * orderQuery.getPageRows())
					+ "," + orderQuery.getPageRows();
			
			
			example.setPageByClause(pageByClause);
		}
		
		List<WOrder> list = null;
		
		try {
			list = orderMapper.selectByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		List<OrderDO> orderList = getOrderDOList(list);
		if(orderList != null) {
			WOrderDetail orderDetail = null;
			List<OrderDO> list2 = new ArrayList<OrderDO>();
			for(OrderDO order : orderList) {
				try {
					
					orderDetail = orderDetailMapper.selectByPrimaryKey(order.getId());
				} catch (Exception e) {
					result.setSuccess(false);
			        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
			        return result;
				}
				order.setOrderDetailDO(getOrderDetailDO(orderDetail));
				list2.add(order);
				
			}
		}
		result.setModel(ResultSupport.FIRST_MODEL_KEY, orderList);
		
		return result;
	}
	
	@Override
	public ResultDO couponList(OrderQuery orderQuery) {
		
		ResultSupport result = new ResultSupport();
		
		WOrderExample example = new WOrderExample();
		WOrderExample.Criteria c = example.createCriteria();
		
		
		if(StringUtil.isGreatOne(orderQuery.getSupplierId())) {
			c.andWOSupplierIdEqualTo(orderQuery.getSupplierId());
		}
	
		c.andWOIsCouponEqualTo(1);
		if(StringUtil.isNotEmpty(orderQuery.getOrderByClause())) {	
			example.setOrderByClause(" " + orderQuery.getOrderByClause() + " " + orderQuery.getSort());
		} else {
			example.setOrderByClause(" CREATE_TIME DESC");
		}
		
		c.andDatalevelEqualTo(1);
		
		if(orderQuery.isPage()) {
			long count = 0;
			try {
				count = orderMapper.countByExample(example);
			} catch (Exception e) {
				result.setSuccess(false);
		        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
		        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
		        e.printStackTrace();
		        logger.error("[obj:member][opt:get][msg:"+e.getMessage()+"]");
		        return result;
			}
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
			int pageNO = orderQuery.getPageNO();
			if(pageNO > 0) {
				pageNO = pageNO -1;
			}
			String pageByClause = " limit " + (pageNO * orderQuery.getPageRows())
					+ "," + orderQuery.getPageRows();
			
			
			example.setPageByClause(pageByClause);
		}
		
		List<WOrder> list = null;
		
		try {
			list = orderMapper.selectByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		List<OrderDO> orderList = getOrderDOList(list);
		if(orderList != null) {
			WOrderDetail orderDetail = null;
			List<OrderDO> list2 = new ArrayList<OrderDO>();
			for(OrderDO order : orderList) {
				try {
					orderDetail = orderDetailMapper.selectByPrimaryKey(order.getId());
				} catch (Exception e) {
					result.setSuccess(false);
			        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
			        return result;
				}
				order.setOrderDetailDO(getOrderDetailDO(orderDetail));
				list2.add(order);
				
			}
		}
		result.setModel(ResultSupport.FIRST_MODEL_KEY, orderList);
		
		
		WOrderExample.Criteria c2 = example.createCriteria();
		
		
		if(StringUtil.isGreatOne(orderQuery.getSupplierId())) {
			c2.andWOSupplierIdEqualTo(orderQuery.getSupplierId());
		}
	
		c2.andWOIsCouponEqualTo(1);
		
		List<WOrder> list2 = new ArrayList<WOrder>();
		
		try {
			list2 = orderMapper.selectByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		BigDecimal feeCount = new BigDecimal(0.00);
		BigDecimal couponCount = new BigDecimal(0.00);
		if(list2.size() != 0) {
			for(WOrder order: list2) {
				feeCount = feeCount.add(order.getwOFee());
				couponCount = couponCount.add(order.getwOCoupon());
			}
			
		}
		
		
		result.setModel(ResultSupport.THIRD_MODEL_KEY, feeCount);
		result.setModel(ResultSupport.FORTH_MODEL_KEY, couponCount);
		return result;
	}
	
	
	@Override
	public ResultDO watiList(Integer supplierId, Integer roleId) {
		
		ResultSupport result = new ResultSupport();
		/*WSupplier supplier = null;
		try {
			supplier = supplierMapper.selectByPrimaryKey(supplierId);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}*/
		
		WOrderExample example = new WOrderExample();
		WOrderExample.Criteria c = example.createCriteria();
		
		if(roleId == 10) {
			c.andWOServiceTypeEqualTo(0).andStatusEqualTo(2);
		} else if(roleId == 11){
			List<Integer> values = new ArrayList<Integer>();
			values.add(1);
			values.add(2);
			values.add(3);
			c.andWOServiceTypeIn(values).andStatusEqualTo(2);
		} else if(roleId == 12) {
			List<Integer> values = new ArrayList<Integer>();
			values.add(11);
			values.add(12);
			c.andWOServiceTypeIn(values).andStatusEqualTo(1);
		}
		example.setOrderByClause(" CREATE_TIME DESC");
		
		c.andWOSupplierIdEqualTo(0).andDatalevelEqualTo(1);
		
		List<WOrder> list = null;
		
		try {
			list = orderMapper.selectByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:order][opt:getWatilist][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		List<OrderDO> orderList = getOrderDOList(list);
		
		if(orderList != null) {
			List<OrderDO> list2 = new ArrayList<OrderDO>();
			for(int i = 0; i < orderList.size(); i++) {
				OrderDO orderDO = orderList.get(i);
				WOrderDetail orderDetail = null;
				try {
					orderDetail = orderDetailMapper.selectByPrimaryKey(orderDO.getId());
				} catch (Exception e) {
					result.setSuccess(false);
			        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
			        return result;
				}
				/*System.out.println(supplier.getwSServuceDistrict());
				if(supplier.getwSServuceDistrict().indexOf(orderDetail.getwODistrict()) > -1) {*/
					orderDO.setOrderDetailDO(getOrderDetailDO(orderDetail));
					list2.add(orderDO);
				/*}*/
				
			}
			result.setModel(ResultSupport.FIRST_MODEL_KEY, list2);
		} else {
			result.setSuccess(false);
		}
		
		
		return result;
	}
	
	class OrderCheck implements Runnable {
		
		public void run() {
			while(true) {
				WOrderExample example = new WOrderExample();
				example.createCriteria().andStatusEqualTo(3);
				List<WOrder> list = null;
				logger.info("开始检查未有分配的订单");
				try {
					list = orderMapper.selectByExample(example);
				} catch (Exception e) {
			        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
			        e.printStackTrace();
				}
				for(WOrder order : list) {
					if(order.getwAssigendTime() != null) {
						long dateline = order.getwAssigendTime().getTime();
						long currentDate = System.currentTimeMillis();
						if((currentDate - dateline) > 86400000l) {
							order.setStatus(6);
							try {
								orderMapper.updateByPrimaryKeySelective(order);
								logger.info("订单号：" + order.getwOOrderNo() + "自动确认");
							} catch (Exception e) {
						        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
						        e.printStackTrace();
							}
						}
					}
				}
				logger.info("结束检查自动确认的订单");
				try {
					Thread.sleep(100000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	class AssignedThread implements Runnable {

		@Override
		public void run() {
			while(true) {
				WOrderExample example = new WOrderExample();
				example.createCriteria().andWOSupplierIdNotEqualTo(0).andStatusEqualTo(2);
				List<WOrder> list = null;
				logger.info("开始检查未有分配的订单");
				try {
					list = orderMapper.selectByExample(example);
				} catch (Exception e) {
			        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
			        e.printStackTrace();
				}
				for(WOrder order : list) {
					if(order.getModifiTime() != null) {
						long dateline = order.getModifiTime().getTime();
						long currentDate = System.currentTimeMillis();
						if((currentDate - dateline) > 300000l) {
							order.setwOSupplierId(0);
							try {
								orderMapper.updateByPrimaryKeySelective(order);
								logger.info("订单号：" + order.getwOOrderNo() + "未有处理");
							} catch (Exception e) {
						        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
						        e.printStackTrace();
							}
						}
					}
				}
				logger.info("结束检查未有分配的订单");
				try {
					Thread.sleep(100000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	
	class Settle implements Runnable {

		@Override
		public void run() {
			while(true) {
				
				try {
				WSupplierExample example = new WSupplierExample();
				example.createCriteria().andDatalevelEqualTo(1);
				List<WSupplier> supplierList = supplierMapper.selectByExample(example);
				SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
				
				String endDate = dateFormat.format(new Date(System.currentTimeMillis() - 604800000));
				System.out.println(endDate);
				for(WSupplier supplier : supplierList) {
					BigDecimal fee = orderMapper.orderStatistics(supplier.getId(), endDate);
					
					BigDecimal smsFee = smsRecordMapper.selectSmsFeeCount(supplier.getId());
					if(smsFee == null) {
						smsFee = new BigDecimal(0.00);
					}
					if(fee != null) {
						System.out.println(fee + ":" + fee.subtract(smsFee));
						List<Integer> orderIds = orderMapper.selectStatisticsOrderId(supplier.getId(), endDate);
						if(orderIds != null) {
							for(Integer id : orderIds) {
								WOrder order = new WOrder();
								order.setId(id);
								order.setStatus(7);
								orderMapper.updateByPrimaryKeySelective(order);
							}
						}
						List<Integer> smsRecordIds = smsRecordMapper.selectSmsFeeId(supplier.getId());
						if(smsRecordIds != null) {
							for(Integer id : smsRecordIds) {
								WSmsRecord smsRecord = new WSmsRecord();
								smsRecord.setId(id);
								smsRecord.setStatus(2);
								smsRecordMapper.updateByPrimaryKeySelective(smsRecord);
							}
						}
						
						WSupplierAccount account = supplierAccountMapper.selectByPrimaryKey(supplier.getId());
						if(fee.compareTo(BigDecimal.ZERO) != 0) {
							fee = fee.subtract(smsFee);
						}
						AccountRecordDO accountRecordDO = new AccountRecordDO();
						accountRecordDO.setwType(1);
						accountRecordDO.setwItem("短信费用收益");
						accountRecordDO.setwMoney(smsFee);
						accountRecordDO.setwDesc("渠道商" + supplier.getwSName() + "结算短信费用金额："+smsFee);
						accountRecordDO.setModifiAuthor("system");
						accountRecordDO.setCreateTime(Calendar.getInstance().getTime());
						accountRecordDO.setCreateAuthor("system");
						accountRecordDO.setStatus(1);
						accountRecordDO.setDatalevel(1);
						supplierAccountService.createRecrod(accountRecordDO);
						logger.info("渠道商" + supplier.getwSName() + "结算金额："+fee);
						BigDecimal balance = account.getwABalance();
						balance = balance.add(fee);
						account.setwABalance(balance);
						account.setwAFee(smsFee);
						supplierAccountMapper.updateByPrimaryKeySelective(account);
					}
				}
				try {
					Thread.sleep(3600000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				} catch (Exception e) {
			        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
			        e.printStackTrace();
				}
			}
			
		}
		
	}

	@Override
	public ResultDO shortOrderList(OrderQuery orderQuery) {
		ResultSupport result = new ResultSupport();
			
		WOrderExample example = new WOrderExample();
		WOrderExample.Criteria c = example.createCriteria();
		
//		if(StringUtil.isGreatOne(orderQuery.getSupplierId())) {
//			c.andWOSupplierIdEqualTo(orderQuery.getSupplierId());
//		}
		c.andWONeedIdIsNull().andDatalevelEqualTo(1);
		
		example.setOrderByClause(" CREATE_TIME DESC");
		
		
		List<WOrder> list = null;
		if(orderQuery.isPage()) {
			long count = 0;
			try {
				count = orderMapper.countByExample(example);
			} catch (Exception e) {
				result.setSuccess(false);
		        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
		        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
		        e.printStackTrace();
		        logger.error("[obj:member][opt:get][msg:"+e.getMessage()+"]");
		        return result;
			}
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
			int pageNO = orderQuery.getPageNO();
			if(pageNO > 0) {
				pageNO = pageNO -1;
			}
			String pageByClause = " limit " + (pageNO * orderQuery.getPageRows())
					+ "," + orderQuery.getPageRows();
			example.setPageByClause(pageByClause);
		}
		
		try {
			list = orderMapper.selectByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:order][opt:getWatilist][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		List<OrderDO> orderList = getOrderDOList(list);
		List<OrderDO> list2 = new ArrayList<OrderDO>();
		for(int i = 0; i < orderList.size(); i++) {
			OrderDO orderDO = orderList.get(i);
			WOrderDetail orderDetail = null;
			try {
				orderDetail = orderDetailMapper.selectByPrimaryKey(orderDO.getId());
			} catch (Exception e) {
				result.setSuccess(false);
		        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
		        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
		        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
		        return result;
			}
			//if(orderDetail!=null){
			//	if(supplier.getwSDistrict().indexOf(orderDetail.getwODistrict()) > -1) {
			//	orderDO.setOrderDetailDO(getOrderDetailDO(orderDetail));
				//	list2.add(orderDO);
			//	}
			//}
			list2.add(orderDO);
		}
		result.setModel(ResultSupport.FIRST_MODEL_KEY, list2);
		
		return result;
	}
	
	
	@Override
	public ResultDO assignList(OrderQuery orderQuery) {
		ResultSupport result = new ResultSupport();
		
		WOrderExample example = new WOrderExample();
		WOrderExample.Criteria c = example.createCriteria();
		
		
		if(StringUtil.isGreatOne(orderQuery.getSupplierId())) {
			c.andWOSupplierIdEqualTo(orderQuery.getSupplierId());
		}
		
		if(orderQuery.getStartDate()!=null  ) {
			c.andCreateTimeGreaterThanOrEqualTo(orderQuery.getStartDate());
		}
		if(orderQuery.getEndDate()!=null){
			c.andCreateTimeLessThanOrEqualTo(orderQuery.getEndDate());
		}
		
        c.andWONeedIdIsNull();
        
        if(StringUtil.isGreatOne(orderQuery.getStatus())) {
        	c.andStatusEqualTo(orderQuery.getStatus());
		}
		
		if(StringUtil.isNotEmpty(orderQuery.getOrderByClause())) {	
			example.setOrderByClause(" " + orderQuery.getOrderByClause() + " " + orderQuery.getSort());
		} else {
			example.setOrderByClause(" CREATE_TIME DESC");
		}
		
		c.andDatalevelEqualTo(1);
		
		if(orderQuery.isPage()) {
			long count = 0;
			try {
				count = orderMapper.countByExample(example);
			} catch (Exception e) {
				result.setSuccess(false);
		        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
		        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
		        e.printStackTrace();
		        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
		        return result;
			}
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
			int pageNO = orderQuery.getPageNO();
			if(pageNO > 0) {
				pageNO = pageNO -1;
			}
			String pageByClause = " limit " + (pageNO * orderQuery.getPageRows())
					+ "," + orderQuery.getPageRows();
			
			
			example.setPageByClause(pageByClause);
		}
		
		List<WOrder> list = null;
		
		try {
			list = orderMapper.selectByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		List<OrderDO> orderList = getOrderDOList(list);
		
		WOrderDetail orderDetail = null;
		List<OrderDO> list2 = new ArrayList<OrderDO>();
		if(orderList != null) {
			for(OrderDO order : orderList) {
				try {
					orderDetail = orderDetailMapper.selectByPrimaryKey(order.getId());
				} catch (Exception e) {
					result.setSuccess(false);
			        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
			        return result;
				}
				order.setOrderDetailDO(getOrderDetailDO(orderDetail));
				list2.add(order);
			}
		}
		result.setModel(ResultSupport.FIRST_MODEL_KEY, list2);
		
		return result;
	}
	
	
	
	@Override
	public ResultDO inspectList(OrderQuery orderQuery) {
		ResultSupport result = new ResultSupport();
		
		WOrderExample example = new WOrderExample();
		WOrderExample.Criteria c = example.createCriteria();
		
		if(orderQuery.getStartDate()!=null  ) {
			c.andCreateTimeGreaterThanOrEqualTo(orderQuery.getStartDate());
		}
		if(orderQuery.getEndDate()!=null){
			c.andCreateTimeLessThanOrEqualTo(orderQuery.getEndDate());
		}
		
		if(StringUtil.isGreatOne(orderQuery.getSupplierId())) {
			c.andWOSupplierIdEqualTo(orderQuery.getSupplierId());
		}
        c.andWONeedIdIsNull();
		c.andStatusEqualTo(9);
	

		if(StringUtil.isNotEmpty(orderQuery.getOrderByClause())) {	
			example.setOrderByClause(" " + orderQuery.getOrderByClause() + " " + orderQuery.getSort());
		} else {
			example.setOrderByClause(" CREATE_TIME DESC");
		}
		
		c.andDatalevelEqualTo(1);
		
		if(orderQuery.isPage()) {
			long count = 0;
			try {
				count = orderMapper.countByExample(example);
			} catch (Exception e) {
				result.setSuccess(false);
		        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
		        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
		        e.printStackTrace();
		        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
		        return result;
			}
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
			int pageNO = orderQuery.getPageNO();
			if(pageNO > 0) {
				pageNO = pageNO -1;
			}
			String pageByClause = " limit " + (pageNO * orderQuery.getPageRows())
					+ "," + orderQuery.getPageRows();
			
			
			example.setPageByClause(pageByClause);
		}
		
		List<WOrder> list = null;
		
		try {
			list = orderMapper.selectByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		List<OrderDO> orderList = getOrderDOList(list);
		
		WOrderDetail orderDetail = null;
		List<OrderDO> list2 = new ArrayList<OrderDO>();
		if(orderList != null) {
			for(OrderDO order : orderList) {
				try {
					orderDetail = orderDetailMapper.selectByPrimaryKey(order.getId());
				} catch (Exception e) {
					result.setSuccess(false);
			        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
			        return result;
				}
				order.setOrderDetailDO(getOrderDetailDO(orderDetail));
				list2.add(order);
			}
		}
		result.setModel(ResultSupport.FIRST_MODEL_KEY, list2);
		
		return result;
	}
	@Override
	public ResultDO successList(OrderQuery orderQuery) {
		
		ResultSupport result = new ResultSupport();
		
		WOrderExample example = new WOrderExample();
		WOrderExample.Criteria c = example.createCriteria();
		
		if(orderQuery.getStartDate()!=null  ) {
			c.andCreateTimeGreaterThanOrEqualTo(orderQuery.getStartDate());
		}
		if(orderQuery.getEndDate()!=null){
			c.andCreateTimeLessThanOrEqualTo(orderQuery.getEndDate());
		}
		
		if(StringUtil.isGreatOne(orderQuery.getSupplierId())) {
			c.andWOSupplierIdEqualTo(orderQuery.getSupplierId());
		}
		c.andStatusEqualTo(6);
	

		if(StringUtil.isNotEmpty(orderQuery.getOrderByClause())) {	
			example.setOrderByClause(" " + orderQuery.getOrderByClause() + " " + orderQuery.getSort());
		} else {
			example.setOrderByClause(" CREATE_TIME DESC");
		}
		
		c.andDatalevelEqualTo(1);
		
		if(orderQuery.isPage()) {
			long count = 0;
			try {
				count = orderMapper.countByExample(example);
			} catch (Exception e) {
				result.setSuccess(false);
		        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
		        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
		        e.printStackTrace();
		        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
		        return result;
			}
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
			int pageNO = orderQuery.getPageNO();
			if(pageNO > 0) {
				pageNO = pageNO -1;
			}
			String pageByClause = " limit " + (pageNO * orderQuery.getPageRows())
					+ "," + orderQuery.getPageRows();
			
			
			example.setPageByClause(pageByClause);
		}
		
		List<WOrder> list = null;
		
		try {
			list = orderMapper.selectByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		List<OrderDO> orderList = getOrderDOList(list);
		
		WOrderDetail orderDetail = null;
		List<OrderDO> list2 = new ArrayList<OrderDO>();
		if(orderList != null) {
			for(OrderDO order : orderList) {
				try {
					orderDetail = orderDetailMapper.selectByPrimaryKey(order.getId());
				} catch (Exception e) {
					result.setSuccess(false);
			        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
			        return result;
				}
				order.setOrderDetailDO(getOrderDetailDO(orderDetail));
				list2.add(order);
			}
		}
		result.setModel(ResultSupport.FIRST_MODEL_KEY, list2);
		
		return result;
	}
	@Override
	public ResultDO failList(OrderQuery orderQuery) {
		
		ResultSupport result = new ResultSupport();
		
		WOrderExample example = new WOrderExample();
		WOrderExample.Criteria c = example.createCriteria();
		
		if(orderQuery.getStartDate()!=null  ) {
			c.andCreateTimeGreaterThanOrEqualTo(orderQuery.getStartDate());
		}
		if(orderQuery.getEndDate()!=null){
			c.andCreateTimeLessThanOrEqualTo(orderQuery.getEndDate());
		}
		
		if(StringUtil.isGreatOne(orderQuery.getSupplierId())) {
			c.andWOSupplierIdEqualTo(orderQuery.getSupplierId());
		}
		c.andStatusEqualTo(5);
	

		if(StringUtil.isNotEmpty(orderQuery.getOrderByClause())) {	
			example.setOrderByClause(" " + orderQuery.getOrderByClause() + " " + orderQuery.getSort());
		} else {
			example.setOrderByClause(" CREATE_TIME DESC");
		}
		
		c.andDatalevelEqualTo(1);
		
		if(orderQuery.isPage()) {
			long count = 0;
			try {
				count = orderMapper.countByExample(example);
			} catch (Exception e) {
				result.setSuccess(false);
		        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
		        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
		        e.printStackTrace();
		        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
		        return result;
			}
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
			int pageNO = orderQuery.getPageNO();
			if(pageNO > 0) {
				pageNO = pageNO -1;
			}
			String pageByClause = " limit " + (pageNO * orderQuery.getPageRows())
					+ "," + orderQuery.getPageRows();
			
			
			example.setPageByClause(pageByClause);
		}
		
		List<WOrder> list = null;
		
		try {
			list = orderMapper.selectByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		List<OrderDO> orderList = getOrderDOList(list);
		
		WOrderDetail orderDetail = null;
		List<OrderDO> list2 = new ArrayList<OrderDO>();
		if(orderList != null) {
			for(OrderDO order : orderList) {
				try {
					orderDetail = orderDetailMapper.selectByPrimaryKey(order.getId());
				} catch (Exception e) {
					result.setSuccess(false);
			        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
			        return result;
				}
				order.setOrderDetailDO(getOrderDetailDO(orderDetail));
				list2.add(order);
			}
		}
		result.setModel(ResultSupport.FIRST_MODEL_KEY, list2);
		
		return result;
	}
	@Override
	public ResultDO acceptList(OrderQuery orderQuery) {
		ResultSupport result = new ResultSupport();
		
		WOrderExample example = new WOrderExample();
		WOrderExample.Criteria c = example.createCriteria();
		
		
		if(StringUtil.isGreatOne(orderQuery.getSupplierId())) {
			c.andWOSupplierIdEqualTo(orderQuery.getSupplierId());
		}
		c.andWONeedIdIsNotNull();
		c.andStatusEqualTo(1);
	

		if(StringUtil.isNotEmpty(orderQuery.getOrderByClause())) {	
			example.setOrderByClause(" " + orderQuery.getOrderByClause() + " " + orderQuery.getSort());
		} else {
			example.setOrderByClause(" CREATE_TIME DESC");
		}
		
		c.andDatalevelEqualTo(1);
		
		if(orderQuery.isPage()) {
			long count = 0;
			try {
				count = orderMapper.countByExample(example);
			} catch (Exception e) {
				result.setSuccess(false);
		        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
		        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
		        e.printStackTrace();
		        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
		        return result;
			}
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
			int pageNO = orderQuery.getPageNO();
			if(pageNO > 0) {
				pageNO = pageNO -1;
			}
			String pageByClause = " limit " + (pageNO * orderQuery.getPageRows())
					+ "," + orderQuery.getPageRows();
			
			example.setPageByClause(pageByClause);
		}
		
		List<WOrder> list = null;
		
		try {
			list = orderMapper.selectByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		List<OrderDO> orderList = getOrderDOList(list);
		
		WOrderDetail orderDetail = null;
		List<OrderDO> list2 = new ArrayList<OrderDO>();
		if(orderList != null) {
			for(OrderDO order : orderList) {
				try {
					orderDetail = orderDetailMapper.selectByPrimaryKey(order.getId());
				} catch (Exception e) {
					result.setSuccess(false);
			        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			        logger.error("[obj:order][opt:get][msg:"+e.getMessage()+"]");
			        return result;
				}
				order.setOrderDetailDO(getOrderDetailDO(orderDetail));
				list2.add(order);
			}
		}
		result.setModel(ResultSupport.FIRST_MODEL_KEY, list2);
		
		return result;
	}
	
	
}
